<template>
  <div class="collection-products">
    <menu-content-layout title="收藏产品">
      <template v-slot:icon>
        <img src="~/assets/images/collection-products1.png" alt="" />
      </template>

      <template v-slot:content>
        <div class="list">
          <div class="item" v-for="(item, index) in list" :key="index" @click="handleDetail(item.product.product_id)">
            <div class="pic">
              <img
                class="objectCover"
                :src="(item.product && item.product.image) || noPic"
                alt=""
              />
            </div>
            <div class="name">
              {{ (item.product && item.product.store_name) || "--" }}
            </div>

            <div class="price">
              <div class="text">
                ￥{{ (item.product && item.product.price) || 0 }}
                <span class="suffix">起</span>
              </div>
              <img
                src="~/assets/images/collectA.png"
                alt=""
                @click.stop="quitCollect(item.type_id)"
              />
            </div>

            <div
              class="mask"
              :style="{ visibility: item.product && !item.product.is_show && 'visible' }"
              @click.stop
            >
              <div class="prompt">该产品已失效，请移除</div>
              <m-button @click="remove(item.product && item.product.product_id)">移除</m-button>
            </div>
          </div>
          <nodata message="暂无收藏商品" :status="listStatus"></nodata>
        </div>

        <m-pagination
          :pagination="total"
          :page-size="limit"
          @changePage="changeThisPage"
        />
      </template>
    </menu-content-layout>
  </div>
</template>

<script>
import MenuContentLayout from "@/components/common/menu-content-layout/MenuContentLayout"
import MPagination from "@/components/common/m-pagination/MPagination"
import MButton from "@/components/common/m-button/MButton"
import { getCollectPro, collectPro } from "@/request/api"

export default {
  name: "CollectionProducts",
  components: { MButton, MPagination, MenuContentLayout },
  data() {
    return {
      list: [],
      page: 1,
      listStatus: 1,
      total: 1,
      limit: 10,
      noPic: require("assets/images/nopic.jpg")
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listStatus = 1
      this.list = []
      let data = {
        page: this.page,
        limit: this.limit
      }
      getCollectPro(data).then(res => {
        this.list = res.data.list
        this.total = res.data.count
        if (this.total === 0) {
          this.listStatus = 0
        } else {
          this.listStatus = 2
        }
        window.scrollTo({ top: 0 })
      }).catch(err => {
        this.listStatus = 2
        this.$toastVant.fail(err.message)
      })
    },
    async quitCollect(id) {
      let params = {
        type_id: id,
        type: 1
      }
      let res = await collectPro(params)
      if(res.status === 200) {
        this.$toastVant.success(res.message)
        this.getList()
      }else {
        this.$toastVant.fail(res.message)
      }
    },
    changeThisPage(page) {
      this.page = page
      this.getList()
    },
    toCateDetails(id, name) {
      this.$router.push({
        path: "/category/classify/" + id + "?name=" + name
      })
    },
    handleDetail(id) {
      this.$router.push('/category/product/'+id)
    }
  }
}
</script>

<style lang="scss" scoped>
@import "~@/assets/style/common.scss";
/deep/ .m-pagination {
  background: #f5f4f9;
}
.collection-products {
  /deep/ .content {
    padding: 0;
  }
  .list {
    width: 100%;
    height: auto;
    background-color: #fff;
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 7px;
    .item {
      width: 22.3%;
      display: inline-flex;
      flex-direction: column;
      position: relative;
      background: #f5f4f9;
      margin: 10px;
      margin-bottom: 14px;
      cursor: pointer;

      .pic {
        height: 174.55px;
        background-color: #ccc;
        margin: 10px;
        margin-bottom: 15px;
      }

      .name {
        height: 42px;
        @include text-omitted(2);
        padding: 0 10px;
        margin-bottom: 10px;
      }

      .price,
      .storage {
        color: $theme-color-1;
        font-size: 16px;
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 0 10px;
        margin-bottom: 10px;
        font-weight: bold;
        .suffix {
          font-size: 14px;
          color: #a9a9a9;
        }

        img {
          width: 18px;
          height: 16px;
          cursor: pointer;
        }
      }

      .company {
        height: 40px;
        background-color: $theme-color-1;
        color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
        margin-top: 10px;
      }

      .mask {
        top: 0;
        bottom: 0;
        left: 0;
        right: 0;
        position: absolute;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        background-color: rgba(102, 102, 102, 0.5);
        visibility: hidden;

        .prompt {
          color: #fff;
          font-size: 14px;
          padding: 0 10px;
          margin-bottom: 15px;
        }
      }
    }
  }
}
</style>
